package com.cloudwise.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.cloudwise.dao.PersonDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2023/2/15 16:08
 * @description：人员dao的实现类
 * @modified By：
 * @version: 1。0
 */
public class PersonDaoImpl implements PersonDao {
    @Override
    public List<Entity> queryAll() {
        List<Entity> list = new ArrayList<>();
        try {
            list = Db.use().query("SELECT * FROM personinfo inner join areainfo a on personinfo.personareaid = a.areaid");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public boolean deletePersonById(Object id) {
        try {
            int del = Db.use().del("personinfo", "personid", id);
            //如果受影响的行数大于0，说明数据库删除成功
            if (del > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean savePerson(Map<String, Object> map) {
        try {
            int insert = Db.use().insert(
                    Entity.create("personinfo")
                            .set("personname", map.get("personname"))
                            .set("personareaid", map.get("personareaid"))
                            .set("personage", map.get("personage"))
                            .set("personaddress", map.get("personaddress"))
                            //new  Date()  获取系统时间
                            .set("createTime", new Date())
            );
            if (insert > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public Entity queryPersonById(Object id) {
        try {
            List<Entity> entityList = Db.use().findAll(Entity.create("personinfo").set("personid", id));
            if (ObjectUtil.isNotNull(entityList) && entityList.size() > 0) {
                return entityList.get(0);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public boolean updatePerson(Map<String, Object> map) {
        try {
            int update = Db.use().update(
                    Entity.create().set("personname", map.get("personname")).set("personareaid", map.get("personareaid"))
                            .set("personage", map.get("personage")).set("personaddress", map.get("personaddress")),
                    Entity.create("personinfo").set("personid", map.get("personid"))
            );
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
}
